<?php
class rateEditAction extends sfActions
{
	public function execute($request)
	{
		$this->rate_id = $request->getParameter('rate_id');
		$rate = Doctrine_Core::getTable("VTNSRate")->findOneByrateId($this->rate_id);

		if ($this->rate_id) {
			$this->rate = Doctrine_Core::getTable('VTNSRate')->find($this->rate_id);
			if ($this->rate) {
				$this->rate_form = new AdminrateForm($this->rate);
			}
		}
        else {
            $this->rate_form = new AdminRateForm();
        }
		if ($request->hasParameter('rate')) {
			$form = $request->getParameter('rate');
			$this->status = $this->updateRateFromRequest($this->rate, $form);
			
			if ($this->status) {
				$this->rate->save();
				$this->redirect('rate/rateList?notify=editSuccess');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
	}
	
	private function updateRateFromRequest(&$rate, $form)
	{
		if ($form['variable_id'])
			$rate->variable_id = $form['variable_id'];
		if (isset($form['min_pax']))
			$rate->min_pax = $form['min_pax'];
		if (isset($form['max_pax']))
			$rate->max_pax = $form['max_pax'];
		if (isset($form['rate_value']))
			$rate->rate_value = $form['rate_value'];
		if (isset($form['is_active']) && $form['is_active'] == "on")
			$rate->is_active = 1;
		else
			$rate->is_active = 0;
		
		return true;
	}
}
?>